package com.example.demo.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.demo.dto.AttendanceDto;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author DUYULONG
 * @date 2023/11/22 10:44
 */
@AllArgsConstructor
@NoArgsConstructor
public class AttendanceListener extends AnalysisEventListener<AttendanceDto> {

    private List<String> rqList = new ArrayList<>();
    private LocalDate startDate;
    private LocalDate endDate;
    private List<String> userList;
    private List<AttendanceDto> dataList = new ArrayList<>();
    private Map<String, List<String>> userDateMap = new HashMap<>();

    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-M-d");
    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/M/d");

    public AttendanceListener(String startTime, String endTime, List<String> userList) {
        this.startDate = LocalDate.parse(startTime);
        this.endDate = LocalDate.parse(endTime);
        this.userList = userList;

        LocalDate tmpDate = startDate;
        while (tmpDate.isBefore(endDate) || tmpDate.compareTo(endDate) == 0) {
            rqList.add(tmpDate.toString());
            tmpDate = tmpDate.plusDays(1L);
        }
    }

    public List<AttendanceDto> getDataList() {
        return dataList;
    }

    Map<String, Integer> dupMap = new HashMap<>();

    @Override
    public void invoke(AttendanceDto data, AnalysisContext context) {
        //过滤用户
        if (CollectionUtils.isNotEmpty(userList)) {
            if (!userList.contains(data.getUsername())) {
                return;
            }
        }

        final String rq = data.getRq();
        if (rq != null) {
            LocalDate date = LocalDate.parse(rq, rq.indexOf("-") > 0 ? formatter1 : formatter2);
            //日期范围
            if (date.isBefore(startDate) || date.isAfter(endDate)) {
                return;
            }
            String key = data.getUsername() + "_" + rq;
            //过滤重复数据
            if (dupMap.get(key) != null) {
                return;
            } else {
                dupMap.put(key, 1);
            }
            data.setRq(date.toString());
            dataList.add(data);

            if (userDateMap.get(data.getUsername()) != null) {
                userDateMap.get(data.getUsername()).add(rq);
            } else {
                userDateMap.put(data.getUsername(), new ArrayList<String>(){{add(rq);}});
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //如果存在userList中有，但是原始数据中没有的，全部新增
        for (String username : userList) {
            if (userDateMap.get(username) == null) {
                rqList.stream().forEach(rq -> {
                    AttendanceDto dto = new AttendanceDto();
                    dto.setUsername(username);
                    dto.setRq(rq);
                    dto.setRemark("全部新增");
                    dataList.add(dto);
                });
            }
        }

        for (String username : userDateMap.keySet()) {
            List<String> dayList = userDateMap.get(username);
            if (dayList.size() != rqList.size()) {
                rqList.stream().forEach(rq -> {
                    if (!dayList.contains(rq)) {
                        AttendanceDto dto = new AttendanceDto();
                        dto.setUsername(username);
                        dto.setRq(rq);
                        dto.setRemark("新增");
                        dataList.add(dto);
                    }
                });
            }
        }

        dataList.sort(Comparator.comparing(a -> LocalDate.parse(a.getRq())));
        System.out.println(dataList);
    }

}
